package org.wmq.organizationmanagement.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wmq.organizationmanagement.mapper.MemberMapper;
import org.wmq.organizationmanagement.model.po.Member;
import org.wmq.organizationmanagement.model.vo.MemberVO;
import org.wmq.organizationmanagement.service.MemberService;
import org.wmq.organizationmanagement.service.OrganizationMasterService;
import org.wmq.organizationmanagement.service.OrganizationMemberService;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    private final OrganizationMemberService organizationMemberService;
    private final OrganizationMasterService organizationMasterService;
    @Override
    public List<MemberVO> getMemberByName(String name) {
        List<Member> memberList = this.lambdaQuery().like(Member::getName, name).list();
        if (memberList == null){
            log.error("查询失败，用户名不存在");
            return null;
        }
        return memberList.stream()
                .map(member -> {
                    MemberVO memberVO = new MemberVO();
                    BeanUtils.copyProperties(member,memberVO);
                    return memberVO;
                }).toList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeMemberById(Long id) {
        this.removeById(id);

        this.organizationMemberService.deleteByMemberId(id);
        this.organizationMasterService.removeByMasterId(id);

        log.info("删除会员成功");

    }
}
